package io.renren.modules.quickService.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.Query;
import io.renren.modules.quickService.constants.UserRight;
import io.renren.modules.quickService.dao.UserRightDao;
import io.renren.modules.quickService.entity.UserRightEntity;
import io.renren.modules.quickService.service.UserRightService;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;


@Service("userRightService")
public class UserRightServiceImpl extends ServiceImpl<UserRightDao, UserRightEntity> implements UserRightService {

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        Page<UserRightEntity> page = this.selectPage(
                new Query<UserRightEntity>(params).getPage(),
                new EntityWrapper<UserRightEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public List<UserRightEntity> getRightList(Long userId) {
        return baseMapper.getRightList(userId);
    }


    @Override
    public void insertOrUpdateUserRight(Long userId, String[] rightList) {
        baseMapper.delete(new EntityWrapper<UserRightEntity>().eq("user_id",userId));
        UserRightEntity userRight;
        for (String rightDesc:rightList
             ) {
            userRight = new UserRightEntity();
            userRight.setUserId(userId);
            userRight.setRightDesc(rightDesc);
            userRight.setRightNo(UserRight.valueOf(rightDesc).getKey());
            userRight.setCreateTime(new Date());
            userRight.setRightStatus("AVAILABLE");
            baseMapper.insert(userRight);
        }
    }

    public void insertOrUpdateUserRight(UserRightEntity userRightEntity) {
        if (userRightEntity.getUserRightId() != null) {
            baseMapper.updateById(userRightEntity);
        } else {
            baseMapper.insert(userRightEntity);
        }
    }
}
